package com.sx.admin.security;

import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lihao on 16/4/19.
 */
public class AuthenticationHolder {
    public static Principal getCurrent() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            String sub = (String) request.getAttribute(Claims.SUBJECT);
            if(sub!=null){
                try {
                    return JSON.parseObject(sub,Principal.class);
                }catch (Exception e){
                }
            }
        }
        return null;
    }
    public static String authPrincipal="authPrincipal";
    public static String authOpenId="authOpenId";
    public static String getOpenId(HttpServletRequest request){
        return (String) CoreWebUtils.getSessionAttribute(request,authOpenId);
    }
    public static void saveOpenId(HttpServletRequest request, String openid){
        CoreWebUtils.setSessionAttribute(request,authOpenId,openid);
    }


}
